/*
 * @Author: xiaoyang
 * @Date: 2021-04-26 09:27:56
 * @LastEditors: xiaoyang
 * @LastEditTime: 2021-04-30 18:09:36
 * @FilePath: \ruoyi-uif:\program_code\loan\main\js\loan.js
 * @description: No description
 */

// loan.js
new Vue({
  el: '#loan',
  data: {
    // tab 选中
    loanType: 1,
    // 还款方式
    dueType: 1,
    // 计算方式
    computerType: 1,
    // 贷款总额
    loanTotalPrice: null,
    // 贷款期限
    loanTimeLength: 20,
    // 新版贷款利率
    loanLPR: 3.25,
    oldLoanLPR: 4.9,
    // 旧版贷款利率
    // 公积金贷款利率
    accumulationFundLoanTitle: 3.25,
    // 基点
    loanBase: 0,
    // 单价
    unitPrice: null,
    // 面积
    area: null,
    // 首付
    downPayment: null,
    // 首付弹窗
    downPaymentShow: false,
    // 首付比例 title 默认为3成
    downPaymentScaleTitle: 3,
    // 首付比例数据
    scaleListData: ['2成', '3成', '4成', '5成', '6成', '7成', '8成', '9成'],
    // LPR ActionSheet actions options
    actions: [
      { name: '按最新LPR', className: 'IS_LPR_ACTIVE', id: 1 },
      { name: '按旧版基准利率', id: 2 },
    ],
    // LPR show
    LPRShow: false,
    // 什么是LPR
    whatLPRShow: false,
    selectLPRTitle: 0,
    // 是不是旧版利率
    oldRateShow: false,
    // 公积金
    // 公积金总额
    accumulationFundTotalPrice: null,
    // 公积金贷款期限
    accumulationFundtimeLength: 20,
  },
  created() {},
  mounted() {
    let data = JSON.parse(Cookies.get('data') || null);
    if (data) {
      for (const key in data) {
        this.$set(this, key, data[key]);
        if (key === 'selectLPRTitle') {
          this.actions.forEach((item) => {
            if (Object.prototype.toString.call(item) === '[object Object]' && item.hasOwnProperty('className')) {
              delete item.className;
            }
          });
          this.actions[data[key]]['className'] = 'IS_LPR_ACTIVE';

          if (data[key] === 1) {
            this.oldRateShow = true;
          } else {
            this.oldRateShow = false;
          }
        }
      }
    }
  },
  methods: {
    /**
     * @description:开始计算按钮,对数据进行校验
     * @param {*}
     * @return {*}
     */
    startComputer() {
      // computerLoan({ loanTotalPrice: this.loanTotalPrice, loanTimeLength: this.loanTimeLength, oldLoanLPR: this.oldLoanLPR });
      // return;
      // window.location.href = `../../result.html?type=${this.loanType}`;
      // 1.先判断为哪个类型的贷款
      if (this.loanType === 1) {
        // 判断为，什么计算方式
        if (this.computerType === 1) {
          // computerType === 1 为按贷款总额进行计算
          if (this.selectLPRTitle === 0) {
            // 按照新版LPR
            let paramsObj = { loanTotalPrice: this.loanTotalPrice, loanLPR: this.loanLPR, loanBase: this.loanBase };
            for (const key in paramsObj) {
              let message = '';
              switch (key) {
                case 'loanTotalPrice':
                  message = '贷款总额';
                  break;
                case 'loanLPR':
                  message = '利率';
                  break;
                case 'loanBase':
                  message = '基点';
                  break;
                default:
                  message = '完整内容';
                  break;
              }
              // 循环判断验证 是否为空
              if (!this[key] && this[key] != 0) {
                this.dialog({ title: '输入提示', message: `请输入${message}` });
                return;
              }
            }
            let p = Number(this.loanLPR) + Number(this.loanBase) / 100;
            computerLoan({ loanTotalPrice: this.loanTotalPrice, loanTimeLength: this.loanTimeLength, oldLoanLPR: p });
          } else if (this.selectLPRTitle === 1) {
            // 按照旧版LPR进行计算
            if (!this.loanTotalPrice) {
              this.dialog({ title: '输入提示', message: `请输入贷款总额` });
              return;
            }
            computerLoan({ loanTotalPrice: this.loanTotalPrice, loanTimeLength: this.loanTimeLength, oldLoanLPR: this.oldLoanLPR });
          }
        } else if (this.computerType === 2) {
          // computerType === 1 为按单价进行计算
          if (this.selectLPRTitle === 0) {
            // 按单价 新版
            console.log('新版');
            let paramsObj = { unitPrice: this.unitPrice, area: this.area, loanLPR: this.loanLPR, loanBase: this.loanBase };
            for (const key in paramsObj) {
              let message = '';
              switch (key) {
                case 'unitPrice':
                  message = '单价';
                  break;
                case 'loanLPR':
                  message = '利率';
                  break;
                case 'loanBase':
                  message = '基点';
                  break;
                case 'area':
                  message = '面积';
                  break;
                default:
                  message = '完整内容';
                  break;
              }
              // 循环判断验证 是否为空
              if (!this[key]) {
                this.dialog({ title: '输入提示', message: `请输入${message}` });
                return;
              }
            }

            let p = Number(this.loanLPR) + Number(this.loanBase) / 100;
            computerLoan({ loanTotalPrice: (this.area * this.unitPrice * (1 - this.downPaymentScaleTitle / 10)) / 10000, loanTimeLength: this.loanTimeLength, oldLoanLPR: p });
          } else if (this.selectLPRTitle === 1) {
            // 按单价旧版
            if (!this.unitPrice) {
              this.dialog({ title: '输入提示', message: '请输入单价' });
              return;
            }

            if (!this.area) {
              this.dialog({ title: '输入提示', message: '请输入面积' });
              return;
            }

            console.log('旧版');
            // console.log(this.area * this.unitPrice * (this.downPaymentScaleTitle / 10));
            // console.log(this.oldLoanLPR);

            computerLoan({ loanTotalPrice: (this.area * this.unitPrice * (1 - this.downPaymentScaleTitle / 10)) / 10000, loanTimeLength: this.loanTimeLength, oldLoanLPR: this.oldLoanLPR });
          }
        }

        window.location.href = `../../result.html?type=${this.loanType}`;
        this.cookiesSetDataCommon();
      } else if (this.loanType === 2) {
        if (!this.accumulationFundTotalPrice) {
          this.dialog({ title: '输入提示', message: '请输入公积金贷款总额' });
          return;
        }
        computerLoan({ loanTotalPrice: this.accumulationFundTotalPrice, loanTimeLength: this.accumulationFundtimeLength, oldLoanLPR: this.accumulationFundLoanTitle });
        window.location.href = `../../result.html?type=${this.loanType}`;
        this.cookiesSetDataCommon();
      } else if (this.loanType === 3) {
        if (!this.loanTotalPrice) {
          this.dialog({ title: '输入提示', message: '请输入商业贷款总额' });
          return;
        }

        if (!this.accumulationFundTotalPrice) {
          this.dialog({ title: '输入提示', message: '请输入公积金贷款总额' });
          return;
        }

        if (this.selectLPRTitle === 0) {
          if (!this.loanLPR) {
            this.dialog({ title: '输入提示', message: '请输入利率' });
            return;
          }

          if (!this.loanBase) {
            this.dialog({ title: '输入提示', message: '请输入基点' });
            return;
          }

          if (!this.accumulationFundTotalPrice) {
            this.dialog({ title: '输入提示', message: '请输入公积金贷款总额' });
            return;
          }
          console.log('新版');
          let p = Number(this.loanLPR) + Number(this.loanBase) / 100;
          let s = computerLoan({ loanTotalPrice: this.loanTotalPrice, loanTimeLength: this.loanTimeLength, oldLoanLPR: p }, this.loanType);
          let a = computerLoan({ loanTotalPrice: this.accumulationFundTotalPrice, loanTimeLength: this.accumulationFundtimeLength, oldLoanLPR: this.accumulationFundLoanTitle }, this.loanType);
          localStorage.setItem('loan', JSON.stringify({ s, a }));
        } else {
          console.log('旧版');
          let s = computerLoan({ loanTotalPrice: this.loanTotalPrice, loanTimeLength: this.loanTimeLength, oldLoanLPR: this.oldLoanLPR }, this.loanType);
          let a = computerLoan({ loanTotalPrice: this.accumulationFundTotalPrice, loanTimeLength: this.accumulationFundtimeLength, oldLoanLPR: this.accumulationFundLoanTitle }, this.loanType);
          localStorage.setItem('loan', JSON.stringify({ s, a }));
        }
        window.location.href = `../../result.html?type=${this.loanType}`;
        this.cookiesSetDataCommon();
      }
    },

    /**
     * @description:切换利率方式
     * @param {*}
     * @return {*}
     */
    chooseLPRType() {
      this.LPRShow = true;
    },

    /**
     * @description:点击取消按钮之后的操作
     * @param {*}
     * @return {*}
     */
    LPRShowCancel() {
      // console.log('object');
    },

    /**
     * @description:选中 ActionsSheet 之后进行的操作 切换选中转态
     * @param {*}    select  选中的对象
     * @return {*}
     */
    LPRShowSelect(select) {
      let data = JSON.parse(Cookies.get('data') || null);
      let index = this.actions.findIndex((item) => item === select);
      if (this.actions[index].className) return;

      this.$nextTick(function () {
        this.selectLPRTitle = index;
      });

      this.actions.forEach((item) => {
        if (Object.prototype.toString.call(item) === '[object Object]' && item.hasOwnProperty('className')) {
          delete item.className;
        }
      });

      if (data) {
        data.selectLPRTitle = index;
        Cookies.set('data', JSON.stringify(data), { expires: 1 });
      }

      this.actions[index]['className'] = 'IS_LPR_ACTIVE';
      this.oldRateShow = !this.oldRateShow;
    },

    /**
     * @description: 选择首付比例
     * @param {*}
     * @return {*}
     */
    chooseDownPayment() {
      this.downPaymentShow = !this.downPaymentShow;
    },

    /**
     * @description: 选中首付比例之后的确定按钮
     * @param {*}
     * @return {*}
     */
    downPaymentConfirm() {
      this.downPaymentShow = !this.downPaymentShow;

      if (this.scale) {
        this.downPaymentScaleTitle = this.scale;
      } else {
        this.downPaymentScaleTitle = 3;
      }
    },

    /**
     * @description: 选中首付比例之后的取消按钮
     * @param {*}
     * @return {*}
     */
    downPaymentCancel() {
      this.downPaymentShow = !this.downPaymentShow;
    },

    /**
     * @description:首付比例变化的问题
     * @param {*} picker
     * @param {*} value
     * @param {*} index
     * @return {*}
     */
    downPaymentChange(picker, value, index) {
      // vant.Toast(value);
      this.scale = parseInt(value);
    },

    /**
     * @description:知道了按钮操作事件
     * @param {*}
     * @return {*}
     */
    gotIt() {
      this.whatLPRShow = !this.whatLPRShow;
    },

    /**
     * @description: 路由跳转
     * @param {*}
     * @return {*}
     */
    routerSwitch() {
      this.cookiesSetDataCommon();
      setTimeout(() => {
        window.location.href = '../../loan-choose.html?type=2';
      }, 200);
    },

    /**
     * @description: 跳转到选择利率页面
     * @param {*}
     * @return {*}
     */
    routerLinkClick() {
      this.cookiesSetDataCommon();
      setTimeout(() => {
        window.location.href = '../../loan-choose.html?type=1';
      }, 200);
    },

    /**
     * @description:
     * @param {*}
     * @return {*}
     */

    tabChange() {
      // 切换的时候进行数据的重置
      let data = JSON.parse(Cookies.get('data') || null);
      this.computerType = 1;

      if (data) {
        data.loanType = this.loanType;
        Cookies.set('data', JSON.stringify(data), { expires: 1 });
      }

      if (this.selectLPRTitle === 0) {
        this.oldRateShow = false;
      } else {
        this.oldRateShow = true;
      }
    },

    /**
     * @description: 抽离的公共函数
     * @param {*}
     * @return {*}
     */
    cookiesSetDataCommon() {
      const { loanType, dueType, computerType, unitPrice, area, downPayment, loanTotalPrice, loanTimeLength, selectLPRTitle, loanLPR, loanBase, oldLoanLPR, accumulationFundTotalPrice, accumulationFundtimeLength, accumulationFundLoanTitle } = this;
      let params = {
        loanType,
        dueType,
        computerType,
        unitPrice,
        area,
        downPayment,
        loanTotalPrice,
        loanTimeLength,
        selectLPRTitle,
        loanLPR,
        loanBase,
        oldLoanLPR,
        accumulationFundTotalPrice,
        accumulationFundtimeLength,
        accumulationFundLoanTitle,
      };
      Cookies.set('data', JSON.stringify(params), { expires: 1 });
    },

    numberFilter: numberFilter,

    /**
     * @description: 自定义弹窗内容
     * @param {*}
     * @return {*}
     */
    dialog({ title, message }) {
      const { Dialog } = vant;
      Dialog.alert({
        title: title,
        message: message,
        confirmButtonColor: '#409EFF',
        className: 'loan-dialog',
      }).then(() => {
        // console.log('on close');
      });
    },
  },
  // 计算属性
  computed: {
    switchComputerType() {
      return this.computerType === 2;
    },
  },
});
